#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Segmentation fault
void get_memory1(char *p)
{
  p = (char *)malloc(100);
}

void test1(void)
{
  char *str = NULL;
  
  get_memory1(str);
  strcpy(str, "hello world");
  printf("%s\n", str);
}

// blank
char *get_memory2(void)
{
  char p[] = "hello world";
  
  return p;
}

void test2(void)
{
  char *str = NULL;

  str = get_memory2();
  printf("%s\n", str);
}

// normal
void get_memory3(char ** p, int num)
{
  *p = (char *)malloc(num);
}

void test3(void)
{
  char *str = NULL;
  
  get_memory3(&str, 100);
  strcpy(str, "hello");
  printf("%s\n", str);
}

// normal
char *get_memory4(void)
{
  return "hello world";
}

void test4(void)
{
  char *str = NULL;

  str = get_memory4();
  printf("%s\n", str);
}


// normal
char *get_memory5(int num)
{
  char *p = (char *)malloc(sizeof(char) * num);
  
  return p;
}

void test5(void)
{
  char *str = NULL;

  str = get_memory5(100);
  strcpy(str, "hello");
  printf("%s\n", str);
  free(str);
}

int main(void)
{
  test3();

  return 0;
}


#if 0
/*
 * intel
 */
00000000000011a9 <get_memory1>:
    11a9:	f3 0f 1e fa          	endbr64 
    11ad:	55                   	push   %rbp
    11ae:	48 89 e5             	mov    %rsp,%rbp
    11b1:	48 83 ec 10          	sub    $0x10,%rsp
    11b5:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    11b9:	bf 64 00 00 00       	mov    $0x64,%edi
    11be:	e8 ed fe ff ff       	callq  10b0 <malloc@plt>
    11c3:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    11c7:	90                   	nop
    11c8:	c9                   	leaveq 
    11c9:	c3                   	retq   

00000000000011ca <test1>:
    11ca:	f3 0f 1e fa          	endbr64 
    11ce:	55                   	push   %rbp
    11cf:	48 89 e5             	mov    %rsp,%rbp
    11d2:	48 83 ec 10          	sub    $0x10,%rsp
    11d6:	48 c7 45 f8 00 00 00 	movq   $0x0,-0x8(%rbp)
    11dd:	00 
    11de:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    11e2:	48 89 c7             	mov    %rax,%rdi
    11e5:	e8 bf ff ff ff       	callq  11a9 <get_memory1>
    11ea:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    11ee:	48 ba 68 65 6c 6c 6f 	movabs $0x6f77206f6c6c6568,%rdx
    11f5:	20 77 6f 
    11f8:	48 89 10             	mov    %rdx,(%rax)
    11fb:	c7 40 08 72 6c 64 00 	movl   $0x646c72,0x8(%rax)
    1202:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1206:	48 89 c7             	mov    %rax,%rdi
    1209:	e8 82 fe ff ff       	callq  1090 <puts@plt>
    120e:	90                   	nop
    120f:	c9                   	leaveq 
    1210:	c3                   	retq   

0000000000001211 <get_memory2>:
    1211:	f3 0f 1e fa          	endbr64 
    1215:	55                   	push   %rbp
    1216:	48 89 e5             	mov    %rsp,%rbp
    1219:	48 83 ec 20          	sub    $0x20,%rsp
    121d:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    1224:	00 00 
    1226:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    122a:	31 c0                	xor    %eax,%eax
    122c:	48 b8 68 65 6c 6c 6f 	movabs $0x6f77206f6c6c6568,%rax
    1233:	20 77 6f 
    1236:	48 89 45 ec          	mov    %rax,-0x14(%rbp)
    123a:	c7 45 f4 72 6c 64 00 	movl   $0x646c72,-0xc(%rbp)
    1241:	b8 00 00 00 00       	mov    $0x0,%eax
    1246:	48 8b 55 f8          	mov    -0x8(%rbp),%rdx
    124a:	64 48 33 14 25 28 00 	xor    %fs:0x28,%rdx
    1251:	00 00 
    1253:	74 05                	je     125a <get_memory2+0x49>
    1255:	e8 46 fe ff ff       	callq  10a0 <__stack_chk_fail@plt>
    125a:	c9                   	leaveq 
    125b:	c3                   	retq   

000000000000125c <test2>:
    125c:	f3 0f 1e fa          	endbr64 
    1260:	55                   	push   %rbp
    1261:	48 89 e5             	mov    %rsp,%rbp
    1264:	48 83 ec 10          	sub    $0x10,%rsp
    1268:	48 c7 45 f8 00 00 00 	movq   $0x0,-0x8(%rbp)
    126f:	00 
    1270:	e8 9c ff ff ff       	callq  1211 <get_memory2>
    1275:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1279:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    127d:	48 89 c7             	mov    %rax,%rdi
    1280:	e8 0b fe ff ff       	callq  1090 <puts@plt>
    1285:	90                   	nop
    1286:	c9                   	leaveq 
    1287:	c3                   	retq   

0000000000001288 <get_memory3>:
    1288:	f3 0f 1e fa          	endbr64 
    128c:	55                   	push   %rbp
    128d:	48 89 e5             	mov    %rsp,%rbp
    1290:	48 83 ec 10          	sub    $0x10,%rsp
    1294:	48 89 7d f8          	mov    %rdi,-0x8(%rbp)
    1298:	89 75 f4             	mov    %esi,-0xc(%rbp)
    129b:	8b 45 f4             	mov    -0xc(%rbp),%eax
    129e:	48 98                	cltq   
    12a0:	48 89 c7             	mov    %rax,%rdi
    12a3:	e8 08 fe ff ff       	callq  10b0 <malloc@plt>
    12a8:	48 89 c2             	mov    %rax,%rdx
    12ab:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    12af:	48 89 10             	mov    %rdx,(%rax)
    12b2:	90                   	nop
    12b3:	c9                   	leaveq 
    12b4:	c3                   	retq   

00000000000012b5 <test3>:
    12b5:	f3 0f 1e fa          	endbr64 
    12b9:	55                   	push   %rbp
    12ba:	48 89 e5             	mov    %rsp,%rbp
    12bd:	48 83 ec 10          	sub    $0x10,%rsp
    12c1:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    12c8:	00 00 
    12ca:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    12ce:	31 c0                	xor    %eax,%eax
    12d0:	48 c7 45 f0 00 00 00 	movq   $0x0,-0x10(%rbp)
    12d7:	00 
    12d8:	48 8d 45 f0          	lea    -0x10(%rbp),%rax
    12dc:	be 64 00 00 00       	mov    $0x64,%esi
    12e1:	48 89 c7             	mov    %rax,%rdi
    12e4:	e8 9f ff ff ff       	callq  1288 <get_memory3>
    12e9:	48 8b 45 f0          	mov    -0x10(%rbp),%rax
    12ed:	c7 00 68 65 6c 6c    	movl   $0x6c6c6568,(%rax)
    12f3:	66 c7 40 04 6f 00    	movw   $0x6f,0x4(%rax)
    12f9:	48 8b 45 f0          	mov    -0x10(%rbp),%rax
    12fd:	48 89 c7             	mov    %rax,%rdi
    1300:	e8 8b fd ff ff       	callq  1090 <puts@plt>
    1305:	90                   	nop
    1306:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    130a:	64 48 33 04 25 28 00 	xor    %fs:0x28,%rax
    1311:	00 00 
    1313:	74 05                	je     131a <test3+0x65>
    1315:	e8 86 fd ff ff       	callq  10a0 <__stack_chk_fail@plt>
    131a:	c9                   	leaveq 
    131b:	c3                   	retq   

000000000000131c <get_memory4>:
    131c:	f3 0f 1e fa          	endbr64 
    1320:	55                   	push   %rbp
    1321:	48 89 e5             	mov    %rsp,%rbp
    1324:	48 8d 05 d9 0c 00 00 	lea    0xcd9(%rip),%rax        # 2004 <_IO_stdin_used+0x4>
    132b:	5d                   	pop    %rbp
    132c:	c3                   	retq   

000000000000132d <test4>:
    132d:	f3 0f 1e fa          	endbr64 
    1331:	55                   	push   %rbp
    1332:	48 89 e5             	mov    %rsp,%rbp
    1335:	48 83 ec 10          	sub    $0x10,%rsp
    1339:	48 c7 45 f8 00 00 00 	movq   $0x0,-0x8(%rbp)
    1340:	00 
    1341:	e8 d6 ff ff ff       	callq  131c <get_memory4>
    1346:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    134a:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    134e:	48 89 c7             	mov    %rax,%rdi
    1351:	e8 3a fd ff ff       	callq  1090 <puts@plt>
    1356:	90                   	nop
    1357:	c9                   	leaveq 
    1358:	c3                   	retq   

0000000000001359 <get_memory5>:
    1359:	f3 0f 1e fa          	endbr64 
    135d:	55                   	push   %rbp
    135e:	48 89 e5             	mov    %rsp,%rbp
    1361:	48 83 ec 20          	sub    $0x20,%rsp
    1365:	89 7d ec             	mov    %edi,-0x14(%rbp)
    1368:	8b 45 ec             	mov    -0x14(%rbp),%eax
    136b:	48 98                	cltq   
    136d:	48 89 c7             	mov    %rax,%rdi
    1370:	e8 3b fd ff ff       	callq  10b0 <malloc@plt>
    1375:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1379:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    137d:	c9                   	leaveq 
    137e:	c3                   	retq   

000000000000137f <test5>:
    137f:	f3 0f 1e fa          	endbr64 
    1383:	55                   	push   %rbp
    1384:	48 89 e5             	mov    %rsp,%rbp
    1387:	48 83 ec 10          	sub    $0x10,%rsp
    138b:	48 c7 45 f8 00 00 00 	movq   $0x0,-0x8(%rbp)
    1392:	00 
    1393:	bf 64 00 00 00       	mov    $0x64,%edi
    1398:	e8 bc ff ff ff       	callq  1359 <get_memory5>
    139d:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    13a1:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    13a5:	c7 00 68 65 6c 6c    	movl   $0x6c6c6568,(%rax)
    13ab:	66 c7 40 04 6f 00    	movw   $0x6f,0x4(%rax)
    13b1:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    13b5:	48 89 c7             	mov    %rax,%rdi
    13b8:	e8 d3 fc ff ff       	callq  1090 <puts@plt>
    13bd:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    13c1:	48 89 c7             	mov    %rax,%rdi
    13c4:	e8 b7 fc ff ff       	callq  1080 <free@plt>
    13c9:	90                   	nop
    13ca:	c9                   	leaveq 
    13cb:	c3                   	retq   

00000000000013cc <main>:
    13cc:	f3 0f 1e fa          	endbr64 
    13d0:	55                   	push   %rbp
    13d1:	48 89 e5             	mov    %rsp,%rbp
    13d4:	e8 dc fe ff ff       	callq  12b5 <test3>
    13d9:	b8 00 00 00 00       	mov    $0x0,%eax
    13de:	5d                   	pop    %rbp
    13df:	c3                   	retq   

/*
 * arm
 */
000000000040068c <get_memory1>:
  40068c:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400690:	910003fd 	mov	x29, sp
  400694:	f9000fa0 	str	x0, [x29, #24]
  400698:	d2800c80 	mov	x0, #0x64                  	// #100
  40069c:	97ffffa5 	bl	400530 <malloc@plt>
  4006a0:	f9000fa0 	str	x0, [x29, #24]
  4006a4:	d503201f 	nop
  4006a8:	a8c27bfd 	ldp	x29, x30, [sp], #32
  4006ac:	d65f03c0 	ret

00000000004006b0 <test1>:
  4006b0:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  4006b4:	910003fd 	mov	x29, sp
  4006b8:	f9000fbf 	str	xzr, [x29, #24]
  4006bc:	f9400fa0 	ldr	x0, [x29, #24]
  4006c0:	97fffff3 	bl	40068c <get_memory1>
  4006c4:	f9400fa2 	ldr	x2, [x29, #24]
  4006c8:	90000000 	adrp	x0, 400000 <_init-0x4f8>
  4006cc:	91250001 	add	x1, x0, #0x940
  4006d0:	aa0203e0 	mov	x0, x2
  4006d4:	f9400022 	ldr	x2, [x1]
  4006d8:	f9000002 	str	x2, [x0]
  4006dc:	b9400821 	ldr	w1, [x1, #8]
  4006e0:	b9000801 	str	w1, [x0, #8]
  4006e4:	f9400fa0 	ldr	x0, [x29, #24]
  4006e8:	97ffffa2 	bl	400570 <puts@plt>
  4006ec:	d503201f 	nop
  4006f0:	a8c27bfd 	ldp	x29, x30, [sp], #32
  4006f4:	d65f03c0 	ret

00000000004006f8 <get_memory2>:
  4006f8:	d10043ff 	sub	sp, sp, #0x10
  4006fc:	90000000 	adrp	x0, 400000 <_init-0x4f8>
  400700:	91250001 	add	x1, x0, #0x940
  400704:	910003e0 	mov	x0, sp
  400708:	f9400022 	ldr	x2, [x1]
  40070c:	f9000002 	str	x2, [x0]
  400710:	b9400821 	ldr	w1, [x1, #8]
  400714:	b9000801 	str	w1, [x0, #8]
  400718:	d2800000 	mov	x0, #0x0                   	// #0
  40071c:	910043ff 	add	sp, sp, #0x10
  400720:	d65f03c0 	ret

0000000000400724 <test2>:
  400724:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400728:	910003fd 	mov	x29, sp
  40072c:	f9000fbf 	str	xzr, [x29, #24]
  400730:	97fffff2 	bl	4006f8 <get_memory2>
  400734:	f9000fa0 	str	x0, [x29, #24]
  400738:	f9400fa0 	ldr	x0, [x29, #24]
  40073c:	97ffff8d 	bl	400570 <puts@plt>
  400740:	d503201f 	nop
  400744:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400748:	d65f03c0 	ret

000000000040074c <get_memory3>:
  40074c:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400750:	910003fd 	mov	x29, sp
  400754:	f9000fa0 	str	x0, [x29, #24]
  400758:	b90017a1 	str	w1, [x29, #20]
  40075c:	b98017a0 	ldrsw	x0, [x29, #20]
  400760:	97ffff74 	bl	400530 <malloc@plt>
  400764:	aa0003e1 	mov	x1, x0
  400768:	f9400fa0 	ldr	x0, [x29, #24]
  40076c:	f9000001 	str	x1, [x0]
  400770:	d503201f 	nop
  400774:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400778:	d65f03c0 	ret

000000000040077c <test3>:
  40077c:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400780:	910003fd 	mov	x29, sp
  400784:	f9000fbf 	str	xzr, [x29, #24]
  400788:	910063a0 	add	x0, x29, #0x18
  40078c:	52800c81 	mov	w1, #0x64                  	// #100
  400790:	97ffffef 	bl	40074c <get_memory3>
  400794:	f9400fa2 	ldr	x2, [x29, #24]
  400798:	90000000 	adrp	x0, 400000 <_init-0x4f8>
  40079c:	91254001 	add	x1, x0, #0x950
  4007a0:	aa0203e0 	mov	x0, x2
  4007a4:	b9400022 	ldr	w2, [x1]
  4007a8:	b9000002 	str	w2, [x0]
  4007ac:	b8402021 	ldur	w1, [x1, #2]
  4007b0:	b8002001 	stur	w1, [x0, #2]
  4007b4:	f9400fa0 	ldr	x0, [x29, #24]
  4007b8:	97ffff6e 	bl	400570 <puts@plt>
  4007bc:	d503201f 	nop
  4007c0:	a8c27bfd 	ldp	x29, x30, [sp], #32
  4007c4:	d65f03c0 	ret

00000000004007c8 <get_memory4>:
  4007c8:	90000000 	adrp	x0, 400000 <_init-0x4f8>
  4007cc:	91250000 	add	x0, x0, #0x940
  4007d0:	d65f03c0 	ret

00000000004007d4 <test4>:
  4007d4:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  4007d8:	910003fd 	mov	x29, sp
  4007dc:	f9000fbf 	str	xzr, [x29, #24]
  4007e0:	97fffffa 	bl	4007c8 <get_memory4>
  4007e4:	f9000fa0 	str	x0, [x29, #24]
  4007e8:	f9400fa0 	ldr	x0, [x29, #24]
  4007ec:	97ffff61 	bl	400570 <puts@plt>
  4007f0:	d503201f 	nop
  4007f4:	a8c27bfd 	ldp	x29, x30, [sp], #32
  4007f8:	d65f03c0 	ret

00000000004007fc <get_memory5>:
  4007fc:	a9bd7bfd 	stp	x29, x30, [sp, #-48]!
  400800:	910003fd 	mov	x29, sp
  400804:	b9001fa0 	str	w0, [x29, #28]
  400808:	b9801fa0 	ldrsw	x0, [x29, #28]
  40080c:	97ffff49 	bl	400530 <malloc@plt>
  400810:	f90017a0 	str	x0, [x29, #40]
  400814:	f94017a0 	ldr	x0, [x29, #40]
  400818:	a8c37bfd 	ldp	x29, x30, [sp], #48
  40081c:	d65f03c0 	ret

0000000000400820 <test5>:
  400820:	a9be7bfd 	stp	x29, x30, [sp, #-32]!
  400824:	910003fd 	mov	x29, sp
  400828:	f9000fbf 	str	xzr, [x29, #24]
  40082c:	52800c80 	mov	w0, #0x64                  	// #100
  400830:	97fffff3 	bl	4007fc <get_memory5>
  400834:	f9000fa0 	str	x0, [x29, #24]
  400838:	f9400fa2 	ldr	x2, [x29, #24]
  40083c:	90000000 	adrp	x0, 400000 <_init-0x4f8>
  400840:	91254001 	add	x1, x0, #0x950
  400844:	aa0203e0 	mov	x0, x2
  400848:	b9400022 	ldr	w2, [x1]
  40084c:	b9000002 	str	w2, [x0]
  400850:	b8402021 	ldur	w1, [x1, #2]
  400854:	b8002001 	stur	w1, [x0, #2]
  400858:	f9400fa0 	ldr	x0, [x29, #24]
  40085c:	97ffff45 	bl	400570 <puts@plt>
  400860:	f9400fa0 	ldr	x0, [x29, #24]
  400864:	97ffff47 	bl	400580 <free@plt>
  400868:	d503201f 	nop
  40086c:	a8c27bfd 	ldp	x29, x30, [sp], #32
  400870:	d65f03c0 	ret

0000000000400874 <main>:
  400874:	a9bf7bfd 	stp	x29, x30, [sp, #-16]!
  400878:	910003fd 	mov	x29, sp
  40087c:	97ffffc0 	bl	40077c <test3>
  400880:	52800000 	mov	w0, #0x0                   	// #0
  400884:	a8c17bfd 	ldp	x29, x30, [sp], #16
  400888:	d65f03c0 	ret
  40088c:	00000000 	.inst	0x00000000 ; undefined


#endif

